package com.blog.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.Cert;
import com.blog.entity.Order;
import com.blog.dao.OrderDao;
import com.blog.entity.OrderTail;
import com.blog.entity.Product;
import com.blog.myUtils.DateTime;
import com.blog.service.CertService;
import com.blog.service.OrderService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private CertService certService;

    @Override
    public List<Order> getOrdersByUid(Integer id) {
        return new Order().selectList(new EntityWrapper<Order>().eq("uid", id));
    }

    @Override
    public Order gengeratOrder(Integer id, HttpSession session) {
        List<Cert> certs = certService.getCertsByUid(id);
        BigDecimal total = new BigDecimal("0");
        Order order = new Order();
        order.setUid(id);
        order.setStatus(Order.STATUS_UNPAY);
        order.setOutTradeNo(UUID.randomUUID().toString());
        order.setCreatedAt(DateTime.getTime());
        order.insert();
        for (Cert cert : certs) {
            BigDecimal num = new BigDecimal(cert.getNum().toString());
            BigDecimal sum = num.multiply(cert.getProduct().getFinalPrice());
            total = total.add(sum);
            OrderTail tail = new OrderTail();
            tail.setNum(cert.getNum());
            tail.setOrderId(order.getId());
            tail.setProductId(cert.getProductId());
            tail.insert();
            cert.deleteById();
        }
        order.setMount(total);
        order.updateById();
        session.setAttribute("cert", certService.getCertsByUid(id));
        return order;
    }

    @Override
    public Order payOrder(String outTradeNo) {
        Order order = new Order().selectOne(new EntityWrapper<Order>().eq("out_trade_no", outTradeNo));
        //TODO:在此记录流水
        order.setStatus(Order.STATUS_PAYED);
        order.updateById();
        //处理库存
        for (OrderTail tail : order.getTails()) {
            Product product = tail.getProduct();
            product.setQty(product.getQty() - tail.getNum());
            product.updateById();
        }
        return order;
    }

    @Override
    public Page<Order> index(Order param, Page<Order> page) {
        return new Order().selectPage(page, "1=1");
    }

    @Override
    public Order create(Order model) {
        return null;
    }

    @Override
    public Order update(Order model) {
//        model.setUpdatedAt(DateTime.getTime());
        if (model.updateById()) {
            return model;
        }
        return null;
    }

    @Override
    public boolean delete(int id) {
        for (OrderTail tail : new Order().selectById(id).getTails()) {
            tail.deleteById();
        }
        return new Order().deleteById(id);
    }

    @Override
    public Order view(int id) {
        return new Order().selectById(id);
    }
}
